import {message} from "ant-design-vue";
export function getUserInfo() {
    return JSON.parse(sessionStorage.getItem("userInfo"));
}

export function dateFormat(fmt, date) {
    let ret;
    const opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 时
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
}

export function parseDatetime(str) {
    const date_ = new Date(str);
    var year = date_.getFullYear();
    var month = date_.getMonth()+1;
    var day = date_.getDate();
    if(month<10) month = "0"+month;
    if(day<10) day = "0"+day;

    var hours = date_.getHours();
    var mins = date_.getMinutes();
    var secs = date_.getSeconds();
    var msecs = date_.getMilliseconds();
    if(hours<10) hours = "0"+hours;
    if(mins<10) mins = "0"+mins;
    if(secs<10) secs = "0"+secs;
    if(msecs<10) secs = "0"+msecs;
    return year+"-"+month+"-"+day+" "+hours+":"+mins+":"+secs;
}
export function showResp(resp) {
    if (resp.code == 200) {
        message.success(resp.message)
    }
}

export function downloadFileUtil(fileName, content) {
    var csvData = new Blob([content], { type: 'application/x-zip-compressed' });
    // for IE
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(csvData, fileName);
    }
    // for Non-IE (chrome, firefox etc.)
    else {
        var a = document.createElement('a');
        document.body.appendChild(a);
        a.style = 'display: none';
        var url = window.URL.createObjectURL(csvData);
        a.href =  url;
        a.download = fileName;
        a.click();
        a.remove();
        window.URL.revokeObjectURL(url);
    }
};


